package com.zhoujing.springsecurity.config;

import com.zhoujing.springsecurity.filter.JwtAuthenticationTokenFilter;
import com.zhoujing.springsecurity.pojo.LoginUser;
import com.zhoujing.springsecurity.service.SysUserService;
import com.zhoujing.springsecurity.service.impl.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * security 构造器
 * @author 周敬
 * @version 1.0
 * @packageName com.zhoujing.springsecurity.config
 * @fileName SecurityConfig
 * @createTime 2022/3/7-17:28-星期一
 * @lastModify 2022/3/7-17:28-星期一
 */
@Configuration
//开启权限控制
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * Spring security 默认使用 PasswordEncoder 注入，但是 PasswordEncoder
     * 使用起来非常的麻烦，所以使用 BCryptPasswordEncoder 代替
     * 创建 BCryptPasswordEncoder 注入容器
     *
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;
    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 对于登录接口 允许匿名访问,登录不能访问
                .antMatchers("/api/login").anonymous()
                //无论登没登录都可以访问
                .antMatchers("/index").permitAll()
                //要访问目标地址，必须要有后面的权限
                .antMatchers("/add").hasAuthority("admin")
                // anyRequest 其他的请求 ，除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();

        //将 jwt 身份验证令牌过滤器 添加到 用户名密码验证过滤器 之前
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        //自定义异常配置
        http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint).
                accessDeniedHandler(accessDeniedHandler);
        //开启跨域
        http.cors();
    }

    /**
     * 用户认证的方法
     * @return
     * @throws Exception
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
